<?php
namespace App\Services\Goods\Goods;

use App\Models\Goods\Goods;
use App\Services\Log\Log\LogLogic;
/**
 * 商品 逻辑层
 * Class GoodsLogic
 * @package App\Services\Goods\Goods
 */
class GoodsLogic
{

    /*商品列表*/
    public function goodsList($data): array
    {
        $_where['is_del'] = 0;
        $re = Goods::where($_where);
        if($data['keywords']){
            $re->where(function($query) use ($data){
                $query->where('title', 'like', '%'.$data['keywords'].'%');});
        }
        $res = $re->paginate(10);
        return auto_return($res);
    }

    /*最新商品列表*/
    public function newGoodsList(): array
    {
        $_where['is_del'] = 0;
        $res = Goods::where($_where)->orderBy('id','desc')->limit(4)->get()->toArray();
        return auto_return($res);
    }
    /*传商品id和数量返回价格*/
    public function goodsPrice($data): array
    {
        $_where['id'] = $data['id'];
        $_where['is_del'] = 0;
        $re = Goods::where($_where)->select()->first();
        $res = $re->price * $data['number'];
        return auto_return($res);
    }

    /*商品详情*/
    public function goodsInfo($data): array
    {
        $_where['id'] = $data['id'];
        $_where['is_del'] = 0;
        $res = Goods::where($_where)->select()->first();
        return auto_return($res);
    }

    /*增*/
    public function addGoods($data): array
    {
        $data['created_at'] = date('Y-m-d H:i:s',time());
        $data['updated_at'] = date('Y-m-d H:i:s',time());
        $res = Goods::insertGetId($data);
        //添加管理员记录
        if ($res) {
            // 写入日志
            $logData['id'] = session('userId');
            $logData['name'] = session('username');
            $logData['type'] = 1;
            $logData['record'] = "商品";
            (new LogLogic())->addLog($logData);
        }
        return auto_return($res);
    }
    /*删*/
    public function delGoods($data): array
    {

        $is_del['is_del'] = 1;
        $is_del['updated_at'] = date('Y-m-d H:i:s',time());
        $res =  Goods::where('id', '=', $data['id'])
            ->update($is_del);
        //添加管理员删除记录
        if ($res) {
            // 写入日志
            $logData['id'] = session('userId');
            $logData['name'] = session('username');
            $logData['type'] = 3;
            $logData['record'] = "商品";
            (new LogLogic())->addLog($logData);
        }
        return auto_return($res, '删除商品成功', '删除商品失败');
    }

    /*改*/
    public function altGoods($data): array
    {
        $_where['id'] = $data['id'];
        $_where['is_del'] = 0;
        $_update['title'] = $data['title'];
        $_update['desc'] = $data['desc'];
        $_update['price'] = $data['price'];
        $_update['remain'] = $data['remain'];
        $_update['updated_at'] = date('Y-m-d H:i:s',time());
        if (isset($data['img_url'])) {
            $_update['img_url'] = $data['img_url'];
        }
        $res =  Goods::where($_where)
            ->update($_update);
        //添加管理员记录
        if ($res) {
            // 写入日志
            $logData['id'] = session('userId');
            $logData['name'] = session('username');
            $logData['type'] = 2;
            $logData['record'] = "商品";
            (new LogLogic())->addLog($logData);
        }
        return auto_return($res);
    }
   
}